/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.OutBillBean;
import com.minierp.bean.OutBillItemBean;
import com.minierp.dao.OutBillDao;
import com.minierp.dao.StockDao;
import com.minierp.model.OutBill;
import com.minierp.model.OutBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class OutBillValidator implements Validator {
	private OutBillDao outBillDao;
	private StockDao stockDao;

	public StockDao getStockDao() {
		return this.stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public boolean supports(Class clazz) {
		return OutBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		OutBillBean bean = (OutBillBean) obj;
		OutBill form = bean.getOutBill();
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("outBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				OutBill hasItems = this.outBillDao.get(form.getBillCode());
				if (hasItems != null && hasItems.getId() != form.getId()) {
					errors.rejectValue("outBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("outBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				SimpleDateFormat arg14 = new SimpleDateFormat("yyyy-MM-dd");
				form.setOutDate(arg14.parse(bean.getOutDateStr()));
			} catch (Exception arg12) {
				hasError = true;
				errors.rejectValue("outDateStr", "",
						"『出库日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			if (form.getStorehouseId() == 0L) {
				errors.rejectValue("outBill.storehouseId", "", "请选择『仓库』");
				hasError = true;
			} else if (form.getManagerId() == 0L) {
				errors.rejectValue("outBill.managerId", "", "请选择『经办人』");
				hasError = true;
			}
		}

		boolean arg15 = false;
		if (!hasError) {
			BigDecimal amountTotal = new BigDecimal(0);
			BigDecimal costTotal = new BigDecimal(0);

			for (int i = 0; i < bean.getItems().size(); ++i) {
				OutBillItemBean cb = (OutBillItemBean) bean.getItems().get(i);
				OutBillItem c = cb.getOutBillItem();
				if (!cb.getStatus().equals("DELETE")) {
					try {
						c.setAmount(new BigDecimal(cb.getAmountStr()));
					} catch (Exception arg13) {
						hasError = true;
						errors.rejectValue("items", "", "出库明细的『数量』只能输入数字");
						break;
					}

					amountTotal = amountTotal.add(c.getAmount());
					costTotal = costTotal.add(c.getCost());
					boolean hasInput = false;
					if (c.getAmount().compareTo(new BigDecimal(0)) != 0
							|| c.getProductId() != 0L || c.getMark() != null
							&& !c.getMark().equals("")) {
						hasInput = true;
					}

					if (hasInput) {
						if (c.getProductId() == 0L) {
							hasError = true;
							errors.rejectValue("items", "", "请选择出库明细的『产品名称』");
							break;
						}

						if (c.getAmount().compareTo(new BigDecimal(0)) == 0) {
							hasError = true;
							errors.rejectValue("items", "", "请输入出库明细的『数量』");
							break;
						}

						arg15 = true;
					} else {
						cb.setStatus("DELETE");
					}
				}
			}

			bean.setAmount(amountTotal);
			bean.setCost(costTotal);
		}

		if (!hasError && !arg15) {
			hasError = true;
			errors.rejectValue("items", "", "出库明细不能为空");
		}

	}

	public OutBillDao getOutBillDao() {
		return this.outBillDao;
	}

	public void setOutBillDao(OutBillDao outBillDao) {
		this.outBillDao = outBillDao;
	}
}